/*
 * Created on 28/mag/07
 *
 * To change the template for this generated file go to
 * Window&gt;Preferences&gt;Java&gt;Code Generation&gt;Code and Comments
 */
package it.unibo.deis.lia.mmhc.test;

import java.util.*;
import java.net.*; //import java.io.*;

import it.unibo.deis.lia.mmhc.*;
import it.unibo.deis.lia.mmhc.com.*;
import it.unibo.deis.lia.mmhc.util.*;
import it.unibo.deis.lia.networkInterface.*;

public class MMHCTestClientReconfiguration extends Thread {

	private static MACMain macMain;

	public static void main(String argv[]) throws Exception {
		MMHCTestClientReconfiguration macTest = new MMHCTestClientReconfiguration();
		macTest.start();
		// macMain=new MACMain();
	}

	public void run() {
		try {
			ConnectorManager connectorManager = ConnectorManager.getInstance();
			Thread.sleep(15000);
			// connectorManager.start();
			// while(true){
			Vector<Channel> suitableChannels = connectorManager
					.getSuitableChannels();
			// System.out.println("MACTest: suitableChannels "+suitableChannels.size());
			Channel channel = null;
			String ipAddress = null;
			while (channel == null || ipAddress == null) {
				for (int i = 0; i < suitableChannels.size() && channel == null; i++) {
					Channel tempChannel = suitableChannels.elementAt(i);
					System.out.println("1 MACTestClient tempChannel " + i + " "
							+ tempChannel);
					// System.out.println("\t#"+i+": "+suitableChannels.elementAt(i));
					GenericNetworkInterface tempGNI = tempChannel
							.getConnector().getLocalNetworkInterface();
					if (tempGNI instanceof Bluetooth) {
						channel = tempChannel;
						ipAddress = tempChannel.getChannelLocalIPAddress();
					}
				}
				System.out.println("1 MACTestClient channel " + channel);
				System.out.println("1 MACTestClient ipAddress " + ipAddress);
				Thread.sleep(250);
				suitableChannels = connectorManager.getSuitableChannels();
			}
			// DatagramSocket ds=new DatagramSocket(4545,);
			System.out.println("MACTestClient ipAddress " + ipAddress);
			// Socket client=new
			// Socket("137.204.57.176",3333,InetAddress.getByName(ipAddress),3333);
			// OutputStream os=client.getOutputStream();

			byte[] buffer;
			DatagramPacket dp;
			DatagramSocket ds = new DatagramSocket(3333, InetAddress
					.getByName(ipAddress));

			try {
				int count = 0;
				boolean ok = true;
				while (ok) {
					// os.write(count);
					buffer = ("" + count).getBytes();
					System.out.println("1 MACTestClient sending "
							+ new String(buffer));
					dp = new DatagramPacket(buffer, buffer.length, InetAddress
							.getByName("137.204.57.176"), 3333);
					ds.send(dp);
					count++;
					suitableChannels = connectorManager.getSuitableChannels();
					ok = false;
					for (int i = 0; i < suitableChannels.size() && ok == false; i++) {
						Channel aChannel = suitableChannels.elementAt(i);
						String anIPAddress = aChannel
								.getChannelLocalIPAddress();
						if (anIPAddress != null
								&& anIPAddress.equals(ipAddress)) {
							ok = true;
						}
					}
					if (ok)
						sleep(250);
				}
			} catch (Exception e) {
				e.printStackTrace();
			}
			ds.close();

			suitableChannels = connectorManager.getSuitableChannels();
			System.out.println("2 MACTest: suitableChannels "
					+ suitableChannels.size());
			channel = null;
			ipAddress = null;
			while (channel == null || ipAddress == null) {
				for (int i = 0; i < suitableChannels.size() && channel == null; i++) {
					// System.out.println("\t#"+i+": "+suitableChannels.elementAt(i));
					Channel tempChannel = suitableChannels.elementAt(i);
					System.out.println("2 MACTestClient tempChannel " + i + " "
							+ tempChannel);
					GenericNetworkInterface tempGNI = tempChannel
							.getConnector().getLocalNetworkInterface();
					if (tempGNI instanceof IEEE80211) {
						channel = tempChannel;
						ipAddress = tempChannel.getChannelLocalIPAddress();
					}
				}
				System.out.println("2 MACTestClient channel " + channel);
				System.out.println("2 MACTestClient ipAddress " + ipAddress);
				Thread.sleep(100);
				suitableChannels = connectorManager.getSuitableChannels();
			}
			// ipAddress=channel.getChannelLocalIPAddress();
			// DatagramSocket ds=new DatagramSocket(4545,);
			// client=new
			// Socket("137.204.57.176",3333,InetAddress.getByName(ipAddress),3333);
			// os=client.getOutputStream();
			ds = new DatagramSocket(3334, InetAddress.getByName(ipAddress));
			try {
				int count = 0;
				while (true) {
					// os.write(count);
					buffer = ("" + count).getBytes();
					System.out.println("2 MACTestClient sending "
							+ new String(buffer));
					dp = new DatagramPacket(buffer, buffer.length, InetAddress
							.getByName("137.204.57.176"), 3333);
					ds.send(dp);
					count++;
					sleep(250);
				}
			} catch (Exception e) {
				e.printStackTrace();
			}
			ds.close();

		} catch (Exception e) {
			e.printStackTrace();
		}
	}
}
